<?php

class HomeController extends Controller {

    public $layout = '//layouts/homemain';
    public $breadcrumbs = true;
    public $homesite = true;

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to access 'index' and 'view' actions.
                'actions' => array('index'),
                'users' => array('@'),
            ),
//            array('deny', // allow all users to access 'index' and 'view' actions.
//                'actions' => array('register', 'login'),
//                'users' => array('?'),
//            ),
            array('allow', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function init() {
        if(Yii::app()->user->isGuest){
            $this->redirect(Yii::app()->baseUrl);
        }
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/common.css');
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/v3/n_common.css');
		 $cs->registerCssFile(Yii::app()->baseUrl . '/css/home.css');
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/common.js", CClientScript::POS_HEAD);
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/v3/n_common.js", CClientScript::POS_HEAD);
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/zhbform.js", CClientScript::POS_HEAD);
        $cs->registerCoreScript('jquery');
    }

    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionIndex() {
        $api = new Api('account');
        $common = new CommonClass();
        $apiurl = "/LdAccountServer/order/getUserOrder";
        $params = array(
            'GetUserOrderReq' => array(
                'userId' => Yii::app()->user->id
            )
        );
        $row = $api->apiclient($apiurl, $params);

        $this->render('index', array('model' => $row, 'common' => $common));
    }

    public function actionAddress() {
        $cs = Yii::app()->clientScript;
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/home.js", CClientScript::POS_HEAD);
        $model = new HomeForm('address');

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if (isset($_POST['HomeForm'])) {
            $model->attributes = $_POST['HomeForm'];
//            if ($model->validate()) {
            echo $model->zipcode;
//            }else{
//                echo 'fff';
//            }
        }

        $this->render('address', array('model' => $model));
    }
    
    public function actionProfile() {
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/jquery.Jcrop.css');
//        $cs->registerCssFile(Yii::app()->baseUrl . '/css/demos.css');
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/jquery.Jcrop.js", CClientScript::POS_HEAD);
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/avatar.js", CClientScript::POS_HEAD);
        
        $model = new ProfileForm();
        
        $userid = Yii::app()->user->id;
        
        if (isset($_POST['ProfileForm'])) {
            $model->attributes = $_POST['ProfileForm'];
//            print_r($model->attributes);
//                print_r($model->validate());
            if ($model->validate() && $_POST['ProfileForm']['url']) {
                
                $url = $model->url;
                $img = Yii::app()->request->hostInfo . Yii::app()->baseUrl . "/".$url;
				$ext = pathinfo($img)['extension'];

                list($olgWidth, $oldHeight) = getimagesize($img);

                $imgX = $model->imgX;
                $imgY = $model->imgY;
                $imgWidth = $model->imgWidth;
                $imgHeight = $model->imgHeight;
                
                $source = imagecreatefromjpeg($img);
                $croped = imagecreatetruecolor($imgWidth, $imgHeight);
                imagecopy($croped, $source, 0, 0, $imgX, $imgY, $imgWidth, $imgHeight);
                
                $path = "uploadfile/".$userid."/".$userid.".".$ext;
                imagejpeg($croped, $path);
                imagedestroy($croped);
                imagedestroy($source);
                
                //缩放比例
                $radio1 = 50/$imgWidth;
                echo $model->createImg($img, $radio1);
                
                $radio2 = 100/$imgWidth;
                echo $model->createImg($img, $radio2);
                
                $radio3 = 200/$imgWidth;
                echo $model->createImg($img, $radio3);
                
                $homePath = Yii::app()->params['homePath'];
				$pathOld = $homePath ."/". $url;
                $path0 = $homePath ."/uploadfile/". $userid. "/". $userid. ".".$ext;
                $path1 = $homePath ."/uploadfile/". $userid. "/". $radio1. ".".$ext;
                $path2 = $homePath ."/uploadfile/". $userid. "/". $radio2. ".".$ext;
                $path3 = $homePath ."/uploadfile/". $userid. "/". $radio3. ".".$ext;
                $apiurl = "/LdAccountServer/userAccount/uploadHeadImg";
                $params = array(
                    'UploadHeadImgReq' => array(
                        'userId' => $userid,
                        'smallUrl' => $path1,
                        'middleUrl' => $path2,
                        'bigUrl' => $path3
                    )                    
                );
                $apitype = "account";
                $api = new Api($apitype);
                $row = $api->apiclient($apiurl, $params);
                unlink($pathOld);
                unlink($path0);
                unlink($path1);
                unlink($path2);
                unlink($path3);
                
            }
            echo "<script language='javascript'>";          
            echo 'window.onload = function(){$("#url").val("");}';
            echo "</script>";
        }

        $this->render('profile', array('model' => $model));
    }    

    public function actionSecurity() {
        $model = new HomeForm('security');

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'security-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        if (isset($_POST['HomeForm'])) {
            $model->attributes = $_POST['HomeForm'];
            if ($model->validate() && $model->updatePassword()) {
                Yii::app()->user->logout();
                $this->redirect(Yii::app()->baseUrl . '/login');
            }
        }

        $this->render('security', array('model' => $model));
    }
    
    public function actionCoupons() {
        $api = new Api('account');
        $this->breadcrumbs = '我的红包';
        $apiurl = "/LdAccountServer/userAccount/getBuyerCoupons";
        $params = array(
            'GetBuyerCouponsReq' => array(
                'buyerId' => Yii::app()->user->id
            )
        );
        $row = $api->apiclient($apiurl, $params);
        
//        print_r($row);
        
        $this->render('coupons', array('row' => $row['GetBuyerCouponsListRsp']['list']));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

}
